package net.lab1024.sa.admin.module.workflow.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.RequiredArgsConstructor;
import net.lab1024.sa.admin.module.workflow.dao.WfCopyDao;
import net.lab1024.sa.admin.module.workflow.domain.bo.WfCopyBo;
import net.lab1024.sa.admin.module.workflow.domain.entity.WfCopy;
import net.lab1024.sa.admin.module.workflow.domain.form.WfCategoryVo;
import net.lab1024.sa.admin.module.workflow.domain.form.WfTaskBo;
import net.lab1024.sa.admin.module.workflow.domain.vo.WfCopyVo;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.RequestUser;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import net.lab1024.sa.base.common.util.StringUtils;
import org.flowable.engine.HistoryService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 流程抄送Service业务层处理
 *
 * @author KonBAI
 * @date 2022-05-19
 */
@RequiredArgsConstructor
@Service
public class WfCopyService {

    @Resource
    private final WfCopyDao wfCopyDao;
    @Resource
    private final HistoryService historyService;

    /**
     * 查询流程抄送
     *
     * @param copyId 流程抄送主键
     * @return 流程抄送
     */
    public WfCopy queryById(Long copyId) {
        return wfCopyDao.selectById(copyId);
    }

    /**
     * 查询流程抄送列表
     *
     * @param bo 流程抄送
     * @return 流程抄送
     */


    public PageResult<WfCopyVo> selectPageList(WfCopyBo bo) {

        Page<?> page = SmartPageUtil.convert2PageQuery(bo);
        List<WfCopyVo> list = wfCopyDao.queryPage(page, bo);

        PageResult<WfCopyVo> pageResult = SmartPageUtil.convert2PageResult(page, list);
        return pageResult;

    }

    /**
     * 查询流程抄送列表
     *
     * @param bo 流程抄送
     * @return 流程抄送
     */

    public List<WfCopyVo> selectList(WfCopyBo bo) {

        return wfCopyDao.queryList(bo);
    }


    public Boolean makeCopy(WfTaskBo taskBo) {
        if (StringUtils.isBlank(taskBo.getCopyUserIds())) {
            // 若抄送用户为空，则不需要处理，返回成功
            return true;
        }
        HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery()
                .processInstanceId(taskBo.getProcInsId()).singleResult();
        String[] ids = taskBo.getCopyUserIds().split(",");
        List<WfCopy> copyList = new ArrayList<>(ids.length);
        RequestUser requestUser = SmartRequestUtil.getRequestUser();
        Long originatorId = requestUser.getUserId();
        String originatorName = requestUser.getUserName();
        String title = historicProcessInstance.getProcessDefinitionName() + "-" + taskBo.getTaskName();
        for (String id : ids) {
            Long userId = Long.valueOf(id);
            WfCopy copy = new WfCopy();
            copy.setTitle(title);
            copy.setProcessId(historicProcessInstance.getProcessDefinitionId());
            copy.setProcessName(historicProcessInstance.getProcessDefinitionName());
            copy.setDeploymentId(historicProcessInstance.getDeploymentId());
            copy.setInstanceId(taskBo.getProcInsId());
            copy.setTaskId(taskBo.getTaskId());
            copy.setUserId(userId);
            copy.setOriginatorId(originatorId);
            copy.setOriginatorName(originatorName);
            copyList.add(copy);
        }
        wfCopyDao.insertBatch(copyList);
        return true;
    }
}
